package com.five.years.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : qinhuajun
 * @Date :2020-03-11
 */
public class MapUtils {

    private static final String request_url = "https://restapi.amap.com/v3/geocode/geo?parameters";

    private static final String request_regeo_url = "https://restapi.amap.com/v3/geocode/regeo?parameters";

    private static final String key = "35439ea599468b4a22894e8a57f709eb";

    public static String[] queryAddressDetail(String address) throws Exception {
        String requestUrl = request_url + "&key=" + key + "&address=" + address;
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.get(requestUrl));
        System.out.println(jsonObject);
        if (StringUtils.isNotNull(jsonObject)) {
            String geocodes = jsonObject.getString("geocodes");
            JSONObject dataJson = JSONArray.parseArray(geocodes).getJSONObject(0);
            String location = dataJson.getString("location");
            return location.split(",");
        } else {
            return null;
        }
    }

   /* public static String queryAddress(String location) throws Exception {
        String requestUrl = request_regeo_url + "&key=" + key + "&location=" + location;
        JSONObject jsonObject = JSONObject.parseObject(HttpUtil.get(requestUrl));
        System.out.println(jsonObject);

        return jsonObject.toJSONString();
    }

    public static void main(String[] args) {
        try {
            String[] address = queryAddressDetail("江苏省苏州市吴江区稻谷互联网产业园");
            System.out.println(address[0]);
            System.out.println(address[1]);

            String location = address[0]+","+address[1];
            System.out.println(location);
            System.out.println(queryAddress(location));
        }catch (Exception ex){

        }

    }*/

    public static <K extends Comparable, V extends Comparable> Map<K, V> sortMapByValues(Map<K, V> aMap) {
        HashMap<K, V> finalOut = new LinkedHashMap<>();
        aMap.entrySet()
                .stream()
                .sorted((p1, p2) -> p2.getValue().compareTo(p1.getValue()))
                .collect(Collectors.toList()).forEach(ele -> finalOut.put(ele.getKey(), ele.getValue()));
        return finalOut;
    }


    /**
     * 使用 Map按value进行排序
     *
     * @param oriMap
     * @return
     */
    public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
                oriMap.entrySet());
        Collections.sort(entryList, new MapValueComparator());

        Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
        Map.Entry<String, Integer> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }

    public static class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {

        @Override
        public int compare(Map.Entry<String, Integer> me1, Map.Entry<String, Integer> me2) {
            //按照从大到小的顺序排列，如果想正序 调换me1 me2的位置
            return me2.getValue().compareTo(me1.getValue());
        }
    }
}
